package com.spring.mvc.controller.converter;

import com.spring.mvc.controller.pojo.GoodsModel;
import org.springframework.core.convert.converter.Converter;
import org.springframework.web.bind.annotation.ControllerAdvice;


/**
 * @author 28593
 * @auther 尚智江
 * @Date 2022/10/30 23:15
 */

public class GoodsConverter implements Converter<String, GoodsModel> {

    @Override
    public GoodsModel convert(String source) {
        Integer a = 3;
        // 创建一个goods实例
        GoodsModel goods = new GoodsModel();
        // 以“，”分隔
        String[] stringValues = source.split(",");
        if(stringValues != null && stringValues.length==a){
            // 为Goods实例赋值
            goods.setGoodsName(stringValues[0]);
            goods.setGoodsPrice(Double.parseDouble(stringValues[1]));
            goods.setGoodsNumber(Integer.parseInt(stringValues[2]));
            return goods;
        }else {
        throw new IllegalArgumentException(String.format("类型转换失败需要格式‘apple，10.58.200’但格式是[%s]",source));
        }

    }


}
